<h1>Cherchez un client </h1>
<form method="post" action="?p=editclient">
	<label for="recherche">
		Recherche :
	</label>
	<input id="recherche" name="recherche" type="text" /><br/>
	
	<input type="submit" value="Cherchez un client"/>
	<input type="reset" value="Annuler">
</form>
<?php

	if (!isAdminConnected()) die('vous n\'étes pas autorisé é voir cette page!');

	$bd = openBD();
	
	if(isset($_POST["recherche"]) && $_POST["recherche"]!=""){
		$like = "%" . $_POST["recherche"] . "%";
		$st = $bd->prepare("SELECT * FROM CLIENT WHERE login LIKE :recherche OR nom LIKE :recherche OR prenom LIKE :recherche OR mail LIKE :recherche");
		$st->bindParam(':recherche', $like);
		$st->execute();
		$Data = $st->fetchColumn();
		if ($Data>0) {
			$stmt = $bd->prepare("SELECT * FROM CLIENT WHERE login LIKE :recherche OR nom LIKE :recherche OR prenom LIKE :recherche OR mail LIKE :recherche");
			$stmt->bindParam(':recherche', $like);
			$stmt->execute();
			$stmt->setFetchMode(PDO::FETCH_ASSOC);
			echo "<table class='rounded-corner'>";
			echo "<tr>";
			echo "<th class='rounded-th-l'>IdClient</th>";
			echo "<th>Login</th>";
			echo "<th>Nom</th>";
			echo "<th>Pr¨¦nom</th>";
			echo "<th>E-mail</th>";
			echo "<th>Montant actuel</th>";
			echo "<th class='rounded-th-r'>Je veut l'editer</th>";
			echo "</tr>";
			while ($row = $stmt->fetch()) {
				echo "<tr>";
				echo "<td>" . $row["idClient"] . "</td>";
				echo "<td>" . $row["login"] . "</td>";
				echo "<td>" . $row["nom"] . "</td>";
				echo "<td>" . $row["prenom"] . "</td>";
				echo "<td>" . $row["mail"] . "</td>";
				echo "<td>" . $row["account"] . "</td>";
				echo "<td class='center'><a href='?p=editclient&idC=" . $row["idClient"] . "'><span class=\"ui-icon ui-icon-check\"></span></a></td>";
				echo "</tr>";
			}
			echo "</table>";
		}
		else{
			?>
			<p class="ui-state-error">Client n'existe pas</p>
			<?php
		}
	}
	
	if(isset($_GET["idC"])){
		$idClient = $_GET["idC"];
		$stm = $bd->prepare("SELECT count(*) FROM CLIENT WHERE idClient = :idClient");
		$stm->bindParam(':idClient', $idClient);
		$stm->execute();
		$resultat = $stm->fetchColumn();
		if ($resultat<1) {
			?>
			<p class="ui-state-error">IdClient n'existe pas</p>
			<?php
		}
		else{
			$stmts = $bd->prepare("SELECT * FROM CLIENT WHERE idClient = :idClient");
			$stmts->bindParam(':idClient', $idClient);
			$stmts->execute();
			$row = $stmts->fetch(PDO::FETCH_ASSOC);
			?>
			<h1>Editez son profile </h1>
			<form method="post" action="?p=editclient">
				<input id="idClient" name="idClient" type="text" style="display:none" value="<?php echo $idClient; ?>" />
				<label for="nom">
					Nom :
				</label>
				<input id="nom" name="nom" type="text" value="<?php echo $row["nom"]; ?>"/><br/>
				<label for="prenom">
					Pr¨¦nom :
				</label>
				<input id="prenom" name="prenom" type="text" value="<?php echo $row["prenom"]; ?>"/><br/>
				<label for="mail">
					E-mail :
				</label>
				<input id="mail" name="mail" type="text" value="<?php echo $row["mail"]; ?>"/><br/>

				<label for="account">
					Montant actuel :
				</label>
				<span>
					<input id="account" name="account" type="text" readonly="readonly" value="<?php echo $row["account"]; ?> €"/>
					<span id="btnChargerCompte" class="ui-icon ui-icon-plusthick" style="position:relative; bottom:20px;left:470px" 
						title="Cliquez pour recharger votre compte"></span>
					<span id="montantACharger" style="position:relative; bottom:38px;left:190px;visibility:hidden">test</span>
				</span>
				<br/>
				<input id="newAccount" name="newAccount" value="0" style="display:none;"/>
				<input type="submit" value="Enregistrer les modifications"/>
				<input type="reset" value="Annuler">
			</form>

			<div id="dialog-charger" title="Chargez votre compte">
			<label for="spinner">Montant :</label>
			<input id="spinner" name="spinner" value="5" />
			</div>
			<?php
		}
	}
	
	if (isset($_POST['nom']) && $_POST['nom']!=""){
		// e-mail unique
		$stC = $bd->prepare("SELECT count(*) FROM CLIENT WHERE mail = :mail LIMIT 1");
		$stC->bindParam(':mail', $_POST["mail"]);
		$stC->execute();
		$DataC = $stC->fetchColumn();
		if ($DataC>0) {
			?>
			<p class="ui-state-error">E-mail déjà utilisé.</p>
			<?php
		}
		else {
			require ('../includes/regExp.php');
			if (validateMail($_POST["mail"]) && validateL($_POST["nom"]) && validateL($_POST["prenom"])){
				try{
				$stS = $bd->prepare("UPDATE CLIENT SET nom=:nom, prenom=:prenom, mail=:mail, account = :account + account WHERE idClient = :idClient");
				$stS->bindParam(':idClient', $_POST["idClient"]);
				$stS->bindParam(':nom', $_POST["nom"]);
				$stS->bindParam(':prenom', $_POST["prenom"]);
				$stS->bindParam(':mail', $_POST["mail"]);
				$stS->bindParam(':account', $_POST["account"]);
				$stS->execute();
				} catch (PDOException $err) {
					echo "Error " . $err->getMessage();
				}
				// r¨¦ussir ¨¤ editer le client
				$stY = $bd->prepare("SELECT * FROM CLIENT WHERE idClient = :idClient");
				$stY->bindParam(':idClient', $_POST["idClient"]);
				$stY->execute();
				$stY->setFetchMode(PDO::FETCH_ASSOC);
				echo "<table class='rounded-corner'>";
				echo "<tr>";
				echo "<th class='rounded-th-l'>IdClient</th>";
				echo "<th>Login</th>";
				echo "<th>Nom</th>";
				echo "<th>Pr¨¦nom</th>";
				echo "<th>E-mail</th>";
				echo "<th>Montant actuel</th>";
				echo "<th class='rounded-th-r'>Je veut l'editer</th>";
				echo "</tr>";
				while ($row = $stY->fetch()) {
					echo "<tr>";
					echo "<td>" . $row["idClient"] . "</td>";
					echo "<td>" . $row["login"] . "</td>";
					echo "<td>" . $row["nom"] . "</td>";
					echo "<td>" . $row["prenom"] . "</td>";
					echo "<td>" . $row["mail"] . "</td>";
					echo "<td>" . $row["account"] . "</td>";
					echo "<td class='center'><a href='?p=editclient&idC=" . $row["idClient"] . "'><span class=\"ui-icon ui-icon-check\"></span></a></td>";
					echo "</tr>";
				}
				echo "</table>";
			}
			else {
				$stR = $bd->prepare("SELECT * FROM CLIENT WHERE idClient = :idClient");
				$stR->bindParam(':idClient', $_POST["idClient"]);
				$stR->execute();
				$row = $stR->fetch(PDO::FETCH_ASSOC);
				?>
				<h1>Editez son profile </h1>
				<form method="post" action="?p=editclient">
					<input id="idClient" name="idClient" type="text" style="display:none" value="<?php echo $_POST["idClient"]; ?>" />
					<label for="nom">
						Nom :
					</label>
					<input id="nom" name="nom" type="text" value="<?php echo $row["nom"]; ?>"/><br/>
					<label for="prenom">
						Prénom :
					</label>
					<input id="prenom" name="prenom" type="text" value="<?php echo $row["prenom"]; ?>"/><br/>
					<label for="mail">
						E-mail :
					</label>
					<input id="mail" name="mail" type="text" value="<?php echo $row["mail"]; ?>"/><br/>

					<label for="account">
						Montant actuel :
					</label>
					<span>
						<input id="account" name="account" type="text" readonly="readonly" value="<?php echo $row["account"]; ?> €"/>
						<span id="btnChargerCompte" class="ui-icon ui-icon-plusthick" style="position:relative; bottom:20px;left:470px" 
							title="Cliquez pour recharger votre compte"></span>
						<span id="montantACharger" style="position:relative; bottom:38px;left:190px;visibility:hidden">test</span>
					</span>
					<br/>
					<input id="newAccount" name="newAccount" value="0" style="display:none;"/>
					<input type="submit" value="Enregistrer les modifications"/>
					<input type="reset" value="Annuler">
				</form>

				<div id="dialog-charger" title="Chargez votre compte">
				<label for="spinner">Montant :</label>
				<input id="spinner" name="spinner" value="5" />
				</div>
				<?php
			}
		}
	}
?>

<script>
$(function() {
	$( "#currency" ).change(function() {
		$( "#spinner" ).spinner({ culture: "en-US" });
	});

	$( "#spinner" ).spinner({
		min: 1,
		max: 500,
		step: 5,
		start: 1000,
		numberFormat: "C"
	});
	
	$( "#dialog-charger" ).dialog({
		autoOpen: false,
		resizable: false,
		height:200,
		width:500,
		modal: true,
		buttons: {
			"Charger votre compte": function() {
				$("#montantACharger").contents().filter(function() {
					return this.nodeType == 3}).replaceWith($("#spinner").val() + " €");
				$("#montantACharger" ).css("visibility", "visible");
				$("#newAccount" ).val($("#spinner").val());
				$( this ).dialog( "close" );
			},
			"Annuler": function() {
				$( this ).dialog( "close" );
			}
		}
	});
	
	$( "#btnChargerCompte" )
		.click(function() {
			$( "#dialog-charger" ).dialog( "open" );
	});
});
</script>